package com.ex.views;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;


public abstract class AbsItem<T> implements ItemListener {
	
	private T viewHolder;
	protected Context context;
	private KAdapter<? extends AbsItem<T>> adapter;
	private int viewType;
	
	public void setViewType(int viewType){
		this.viewType = viewType;
	}
	
	public int getViewType(){
		return viewType;
	}
	
	public KAdapter<? extends AbsItem<T>> getAdapter(){
		return adapter;
	}
	
	public AbsItem(Context ctx){
		context = ctx;
	}
	
//	public AbsItem(KAdapter<? extends AbsItem<T>> adapter){
//		this();
//		this.adapter = adapter;
//	}
	
	public AbsItem(KAdapter<? extends AbsItem<T>> adapter, Context ctx){
		this(ctx);
		this.adapter = adapter;
	}
	
	public void setKAdapter(KAdapter<? extends AbsItem<T>> adapter){
		this.adapter = adapter;
	}
	
//	public AbsItem(){
//		context = KtvApplication.getInstance();
//	} 
	
	public abstract View initViews(Context context, int position, ViewGroup parent);
	
	public abstract T initViewHolder(View view);
	
	public abstract void updateViews(View view, T viewHolder, int position, ViewGroup parent);

	@Override
	public View onGetView(View convertView, int position, ViewGroup parent) {
		if(convertView == null){
			convertView = initViews(context, position, parent);
			viewHolder = initViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else{
			viewHolder = (T)convertView.getTag();
		}
		updateViews(convertView, viewHolder, position, parent);
		return convertView;
	}
}
